public class DecoratorPattern {
 
    public static void main(String[] args) {
        Cup cup=new Cup();
        Decorator pad=new CupPad();
        Decorator cover=new CupCover();
        /* 
         * show函数的调用顺序是 CupPad的show、Decorator的show、CupCover的show、Decorator的show、Cup的show
         */
        pad.setCup(cup);
        cover.setCup(pad);
        cover.show();
    }
}

class Cup {
    public void show() {
        System.out.println("杯子 ");
    }
}
/* 
 * 通过component属性，可以形成一条装饰链，
 */
class Decorator extends Cup {
    Cup component;

    void setCup(Cup component) {
        this.component = component;
    }
    @Override
    public void show() {
        if (component != null) {
            component.show();
        }
    }
}
class CupPad extends Decorator{
    @Override
    public void show(){
        System.out.print("杯垫 ");
        super.show();
    }
}
class CupCover extends Decorator{
    @Override
    public void show(){
        System.out.print("盖子 ");
        super.show();
    }
}